package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 地址管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    /**
     * 添加收货地址
     *
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping
    public R<String> addressBook(@RequestBody AddressBook addressBook, HttpSession session) {
        log.info("addressBook------------{}", addressBook);
        Long userId = (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     * 展示当前用户的收货地址
     *
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * 默认地址的设置
     *
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping("/default")
    public R<String> defau(@RequestBody AddressBook addressBook, HttpSession session) {
        Long id = addressBook.getId();
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(lqw);
        List<AddressBook> collect = list.stream().map((lis) -> {
            if (lis.getId().equals(id)) {
                lis.setIsDefault(1);
                return lis;
            } else {
                lis.setIsDefault(0);
                return lis;
            }
        }).collect(Collectors.toList());
        addressBookService.saveOrUpdateBatch(collect);
        return R.success("设置成功");
    }

    /**
     * 修改地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> as(@PathVariable Long id) {
        log.info("aaaaaaaaaaaaaa:{}", id);
    /*   LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
       lqw.eq(AddressBook::getId,id);
       AddressBook addressBook = addressBookService.getOne(lqw);*/
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 保存修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> put(@RequestBody AddressBook addressBook) {
        addressBookService.removeById(addressBook.getId());
        addressBookService.save(addressBook);
        return R.success("修改成功");
    }


}
